package com.xafc.demo.framework.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 公用插件方法
 */
public class CommonUtils {


    /**
     * 根据上个编号生成下个编号
     * @param prevNumber 上个编号
     * @return 下个编号
     */
    public static String genNextNumber(String prevNumber) {
        final int next = Integer.parseInt(prevNumber) + 1;
        return StrUtil.repeat("0", prevNumber.length() - (next + "").length()) + next;
    }

    /**
     * 获取请求ip
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if( ip.indexOf(",")!=-1 ){
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取请求ip
     * @return
     */
    public static String getIpAddrByRequestHolder() {
        HttpServletRequest request = CommonUtils.getServletRequest();
        return getIpAddr(request);
    }

    /**
     * 获取当前线程的HttpServletRequest对象
     * @return
     */
    public static HttpServletRequest getServletRequest(){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        return ((ServletRequestAttributes)requestAttributes).getRequest();
    }

    /**
     *  获取当前线程的HttpServletResponse对象
     * @return
     */
    public static HttpServletResponse getServlettResponse(){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        return ((ServletRequestAttributes)requestAttributes).getResponse();
    }

    /**
     * 获取上下文路径
     * @return
     */
    public static String getContextPath(){
        HttpServletRequest request = CommonUtils.getServletRequest();
        return request.getContextPath();
    }
}
